package cn.amebius.thread;

import cn.amebius.util.DockerClientUtil;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.core.command.LogContainerResultCallback;

import java.io.Closeable;
import java.io.IOException;

/**
 * @author 阿伟
 * @date 2023/7/24 19:05
 */
public class LogConsoleThread extends Thread{

    @Override
    public void run() {
        DockerClient dockerClient = DockerClientUtil.getDockerClient();
        LogContainerCmd lcc = dockerClient.logContainerCmd(DockerClientUtil.getContainerId());
        try {
            lcc.withStdErr(true)
                    .withStdOut(true)
                    .withFollowStream(true)
                    .exec(new LogContainerResultCallback() {
                        @Override
                        public void onNext(Frame item) {
                            System.out.println("【Log日志】 "+ new String(item.getPayload()));
                        }
                    }).awaitCompletion();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
